home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.lang.c.moderated
- Subject: Re: const pointer confusion...
- Date: 25 Mar 1996 06:24:21 -0600
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4j639l$3ju@solutions.solon.com>
- References: <4j06gm$7oa@solutions.solon.com> <4j41hs$nku@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4j41hs$nku@solutions.solon.com>,
- The Amorphous Mass <robinson@blue.weeg.uiowa.edu> wrote:
- >> 2) int *const p;
- >> p is a pointer to an integer. *p can be assigned to, but p can
- >> never be made to point to another address in memory, right?
- >
- > This is a syntax error.
-
- No it is not. He is right in his summary of the semantics, but not in the name:
- p is a constant pointer to an integer.
-
- >> 3) int const *p;
- >> What the heck is this? I can't find anything like this in my
- >> books, but my compiler thinks everything is hunky doory!???
- >
- > This is a constant pointer to an integer. *p can be changed, but p
- >cannot.
-
- Wrong again: ``int const'' is a permutation of ``const int'', a syntactic
- variant of the same semantic declaration specifier list. Both refer to a
- constant integer. The ``*p'' is a declarator which makes p a pointer to such a
- constant integer.
-
- Look at the grammar productions for the syntactic unit
- ``declaration-specifiers'', K&R2 p. 210. It clearly shows that storage class
- specifiers, type qualifiers and type specifiers may appear in any order. There
- are some semantic restrictions on this syntactic freedom: a storage class
- specifier may appear only once, for instance, as may a type specifier:
-
- int typedef extern auto foo;
-
- is syntactically valid, but semantically incorrect, because it has three
- storage specifiers.
-
- Pump that through GCC, for instance, and you don't get a syntax error, but a
- semantic error warning you that you have more than one datatype in the
- declaration, as well as more than one storage class. How the specifiers are
- ordered doesn't matter: ``int typedef x;'' is just as valid as
- ``typedef int x;''
-
- > Also, there's
- >
- > 4) const int const *p;
- >
- > Which is a constant pointer to a constant integer; neither *p nor
- >p can change.
-
- Wrong. ``const int const'' is just a redundancy. It is the same as ``const
- int'' or ``int const''. A well-written compiler should warn you that you have
- the same type qualifier appearing twice.
-
- In fact I just tried it with GCC and, indeed!, it warns about ``duplicate
- const'' in the declaration! Hooray!
-
-
- I think you were trying to say:
-
- 4) const int * const p;
- --
-